class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> result(n, vector<int>(n, 0));
        int startx = 0, starty = 0;
        int loop = n / 2;
        int mid = n / 2;
        int offset = 1;
        int count = 1;
        while(loop--){
            int i = startx;
            int j = starty;
            for (; j < n - offset; j++){
                result[i][j] = count++;
            }
            for (; i < n - offset; i++){
                result[i][j] = count++;
            }

            for (; j>startx; j--){
                result[i][j] = count++;
            }

            for (; i > starty; i--){
                result[i][j] = count++;
            }
            
            startx++;
            starty++;
            offset++;
        }
        if(n % 2){
            result[mid][mid] = count;
        }
       return result;
    }
};